knitr::opts_chunk$set(echo = TRUE, tidy.opts=list(width.cutoff=80),tidy=TRUE)
# R version 4.2.2 (2022-10-31) -- "Innocent and Trusting"
#install packages
#install.packages(c("memisc", "tidyverse"))
#load required libraries
library(memisc) # memisc_0.99.22
library(tidyverse) # tidyverse_1.3.0
library(formatR)
# set working directory
#setwd("")
#load datasets
## conjoint data
master <- read_csv("~/Dropbox/women_compaign_project/Paper1/jop_submission/revised_paper/replication_files/mm_conjoint_data.csv")
## respondents data
psat <- read_csv('~/Dropbox/women_compaign_project/Paper1/jop_submission/revised_paper/replication_files/mm_respondent_data.csv')
conjoint_dat <- data.set(master)
class(conjoint_dat)
names(conjoint_dat) <- names(master)
names(master)
## conjoint data
conjoint_dat <- data.set(master)
class(conjoint_dat)
names(conjoint_dat) <- names(master)
conjoint_dat <- within(conjoint_dat, {
description(round_num) <- "Conjoint round"
description(rd_rand_a_party) <-  "Party of candidate"
description(rd_rand_a_promises) <- "Candidate policy focus"
description(rd_rand_a_education) <- "Level of education of candidate"
description(rd_rand_a_gender) <- "Gender of candidate"
description(rd_rand_a_profession) <-  "Profession/occupation of candidate"
description(PARENT_KEY) <- "Respondent ID"
description(KEY) <-"Respondent-conjoint-profile round (pairs) ID"
description(candidate) <- "Candidate number in pair"
description(outcome_binary_resp) <- "Whether respondent will vote for candidate in profile"
description(outcome_binary_other) <- "Whether others will vote for candidate in profile"
description(resp_pref_first) <- "Whether respondent's was randomly assigned to choose their preferred candidate first"
foreach(x=c(round_num,rd_rand_a_party,
rd_rand_a_promises,rd_rand_a_education,
rd_rand_a_gender,rd_rand_a_profession,
candidate,outcome_binary_resp,
outcome_binary_other,resp_pref_first),
{measurement(x) <- "ordinal"                                    })
labels(round_num) <- c("First"      = 1,
"Second"     = 2,
"Third"      = 3,
"Fourth"     = 4,
"Fifth" = 5,
"Sixth" = 6)
labels(rd_rand_a_party) <-  c("Independent"= 0 , "Minor"= 1, "Major"=2)
labels(rd_rand_a_promises) <- c("Boreholes"=0, "Education"=1, "Roads"=2)
labels(rd_rand_a_education) <- c("Secondary"=0, "University"=1)
labels(rd_rand_a_gender) <- c("Male"=0, "Female"=1)
labels(rd_rand_a_profession) <-  c("Teacher"=0, "Maize farmer"=1,"Major business owner"=2)
labels(candidate) <- c("First"= 1, "Second"= 2)
labels(outcome_binary_resp) <- c("No"= 0, "Yes"= 1)
labels(outcome_binary_other) <- c("No"= 0, "Yes"= 1)
labels(resp_pref_first) <- c("No"= 0, "Yes"= 1)
})
## personal data
resp_dat <- data.set(psat)
names(resp_dat) <- names(psat)
names(psat)
table(psat$q24)
table(psat$q27)
table(psat$q28)
## personal data
resp_dat <- data.set(psat)
names(resp_dat) <- names(psat)
resp_dat <- within(resp_dat, {
description(SubmissionDate) <- "Interview date"
description(KEY) <- "Respondent ID"
description(enumerator) <- "Enumerator name"
description(q2) <- "Constituency name"
description(region) <- "Region name"
description(party_order) <- "Order of display of conjoint attribute: Party"
description(promises_order) <- "Order of display of conjoint attribute: Policy focus"
description(education_order) <- "Order of display of conjoint attribute: Education"
description(gender_order) <- "Order of display of conjoint attribute: Gender"
description(profession_order) <- "Order of display of conjoint attribute: Profession/occupation"
description(q5_age) <- "Respondent age"
description(female) <- "Respondent gender (female=1)"
description(primary_or_less) <- "Education level"
description(employed) <- "Employment status"
description(correctMPname) <- "Respondent correctly named constituency's Member of Parliament"
description(correctMPparty) <- "Respondent correctly named party of constituency's Member of Parliament"
description(close_to_a_party) <- "Respondent report being close to a party"
description(party_dpp) <- "Party close to: DPP"
description(party_mcp) <- "Party close to: MCP"
description(party_utm) <- "Party close to: UTM"
description(turnout_last_elect) <- "Respondent voted in last election"
description(own_radio ) <- "Asset ownership: radio"
description(own_tv) <- "Asset ownership: television"
description(own_motor) <- "Asset ownership: motorcycle"
description(own_mobile  ) <- "Asset ownership: mobile phone "
description(total_assets) <- "Total asset ownership"
description(ethnic_chewa) <- "Ethnicity: Chewa"
description(ethnic_lomwe) <- "Ethnicity: Lomwe"
description(ethnic_yao) <- "Ethnicity: Yao"
description(conservative) <- "Hold conservative attitude towards women"
description(tr_video) <- "Video treatment"
description(q4) <- "Respondent gender (orginal)"
description(q23) <- "What message did you take from the video?"
description(q24) <- "Which of the following do you think best describe the video"
description(q27) <- "Do you feel close to any particular party?"
description(q28) <- "What party is that?"
description(validdata) <- "Whether data was part of pilot survey"
description(q19_v) <- "In parliamentary elections, it is better to vote for a man than a woman because the man is more likely to win."
description(q20_v) <- "After the 2025 elections, we will have more women MPs than we have today"
description(q21_v) <- "A woman running for parliament in my constituency is likely to be unsuccessful as she would face discriminations from parties or voters."
description(q22_v) <- "Men make better political leaders than women"
description(tr_video_lv) <- "Video treatment(change reference category)"
foreach(x=c(region,q2, q4,party_order,promises_order,
education_order,gender_order,profession_order,q19_v, q20_v,q21_v, q22_v,q23,q24,q27,q28,tr_video,female,
primary_or_less,employed,agric_worker,correctMPname,correctMPparty,close_to_a_party,
party_dpp,party_mcp,party_utm,
turnout_last_elect,own_radio,own_tv,own_motor,own_mobile,
total_assets,ethnic_chewa,ethnic_lomwe,
ethnic_yao ,ethnic_sena ,conservative,validdata,tr_video_lv),
{measurement(x) <- "ordinal"                                    })
labels(region) <- c("Central" =2, "Southern"=3)
labels(q2) <-  c("Nkhotakota South East" = 47,
"Nkhotakota North East" = 44,
"Ntchisi South"=49,
"Ntchisi North"=50,
"Salima Central"=60,
"Salima North"=59,
"Chiradzulu East"=143,
"Chiradzulu South"=140,
"Nsanje North"=193,
"Nsanje Central"=191,
"Phalombe  Central"=170,
"Phalombe South"=169)
labels(q4) <- c("Male" =1, "Female"=2, "Prefer not to say" =3,                                            "Missing" = NA)
labels(q19_v) <-  c("Strongly agree" = 1,
"Agree" =2,
"Disagree" =3,
"Strongly disagree"=4,
"Refuse to answer" = -999,
"Do not know [do not read]" = 999,                                            "Missing" = NA)
labels(q20_v) <- c("Strongly agree" = 1,
"Agree" =2,
"Disagree" =3,
"Strongly disagree"=4,
"Refuse to answer" = -999,
"Do not know [do not read]" = 999,                                            "Missing" = NA)
labels(q21_v) <-  c("Strongly agree" = 1,
"Agree" =2,
"Disagree" =3,
"Strongly disagree"=4,
"Refuse to answer" = -999,
"Do not know [do not read]" = 999,                                            "Missing" = NA)
labels(q22_v) <- c("Strongly agree" = 1,
"Agree" =2,
"Disagree" =3,
"Strongly disagree"=4,
"Refuse to answer" = -999,
"Do not know [do not read]" = 999,                                            "Missing" = NA)
labels(female) <-  c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(primary_or_less) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(employed) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(agric_worker) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(correctMPname) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(correctMPparty) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(close_to_a_party) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(party_dpp) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(party_mcp) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(party_utm) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(turnout_last_elect) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(own_radio) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(own_tv) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(own_motor) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(own_mobile) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(total_assets) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(ethnic_chewa) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(ethnic_lomwe) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(ethnic_yao ) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(ethnic_sena ) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(conservative) <- c("No"=0, "Yes"=1,                                            "Missing" = NA)
labels(q24) <- c("Voting for a woman running for MP is often a waste of one’s vote, as she is unlikely to win" = 1,
"Voting for a woman running for MP can help her win"= 2,
"None of the above"=3,
"Refuse to answer"=998,
"Do not know [do not read]"=999)
labels(q27) <- c("No (Does not feel close to ANY party)" =1,
"Yes (Feel close to a party)" =2,
"Refused to answer (do not read)" = -999,
"Do not know [do not read] (do not read)" = 999)
labels(q28) <- c("Alliance for Democracy (AFORD)"=	1,
"Democratic Progressive Party (DPP)"=	2,
"Malawi Forum for Unity and Development (MAFUNDE)"=	3,
"Malawi Congress Party (MCP)"=	4,
"National Salvation Front (NSF)"=	5,
"New Rainbow Coalition Party (NARC)"=		6,
"People’s Democratic Movement (PDM)"=		7,
"People’s Party (PP)"=		8,
"People’s Progressive Movement (PPM)"	=	9,
"People’s Transformation Party (PETRA)"	=	10,
"Republican Party (RP)"	=	11,
"United Democratic Front (UDF)"	=	12,
"New Labor Party (NLP)"=		13,
"Chipani Cha Fuko (CCP)"	=	14,
"United Independent  Party (UIP)"	=	15,
"Tonse Alliance (MCP-UTM)"	=	16,
"DPP-UDF Alliance" =		17,
"United Transformation Movement (UTM)"	=	18,
"Mbakuwaku Movement for Development (MMD)"=		19,
"Other"	=	99,
"Refused to answer"	=	-999,
"Do not know [do not read]"	=	999)
})
library(factorEx)
library(devtools)
install.packages("factorEx")
install_github("naoki-egami/factorEx", dependencies=TRUE)
library(factorEx)
install.packages("factorEx")
library(igraph)
install.packages("igraph")
#install.packages("factorEx")
library(factorEx)
library(tidyverse)
master_ps <- read_csv("~/Dropbox/women_compaign_project/Paper1/jop_submission/revised_paper/replication_files/master_ps.csv")
names(master_ps)
gender_dist_marginals <- list(gender=c("Male"=0.76, "Female"=0.24),party=c("Independent"=0.56, "Minor"= 0.26, "Major"=0.26), promises= c("Boreholes"=0.33, "Education"=0.33, "Roads"= 0.33), education=c("Secondary"=0.55, "University"=0.45), profession=c("Teacher"=0.42, "Maize farmer"=0.05, "Major business owner"=0.53))
sudat <- master_ps[,c("outcome_binary_resp","gender","party","promises","education","profession","KEY","PARENT_KEY","tr_video_lv")]
sudat <-na.omit(sudat)
sudat$gender <- factor(sudat$gender,levels = c("Male", "Female"))
sudat$party <- as.factor(sudat$party)
sudat$promises <- as.factor(sudat$promises)
sudat$education <- as.factor(sudat$education)
sudat$profession <- as.factor(sudat$profession)
sudat$pair_id <- as.integer(substr(sudat$KEY, nchar(sudat$KEY) - 1, nchar(sudat$KEY) - 1))
sudat <- sudat[order(sudat[,8], sudat[,10] ), ]
names(sudat)
master_ps <-read_csv(~/Dropbox/women_compaign_project/data/master_ps)
master_ps <-read_csv("~/Dropbox/women_compaign_project/data/master_ps")
names(master_ps)
gender_dist_marginals <- list(gender=c("Male"=0.76, "Female"=0.24),party=c("Independent"=0.56, "Minor"= 0.26, "Major"=0.26), promises= c("Boreholes"=0.33, "Education"=0.33, "Roads"= 0.33), education=c("Secondary"=0.55, "University"=0.45), profession=c("Teacher"=0.42, "Maize farmer"=0.05, "Major business owner"=0.53))
sudat <- master_ps[,c("outcome_binary_resp","gender","party","promises","education","profession","KEY","PARENT_KEY","tr_video_lv")]
sudat <-na.omit(sudat)
names(master_ps)
names(sudat)
sudat$gender <- factor(sudat$gender,levels = c("Male", "Female"))
sudat$party <- as.factor(sudat$party)
sudat$promises <- as.factor(sudat$promises)
sudat$education <- as.factor(sudat$education)
sudat$profession <- as.factor(sudat$profession)
sudat$pair_id <- as.integer(substr(sudat$KEY, nchar(sudat$KEY) - 1, nchar(sudat$KEY) - 1))
sudat <- sudat[order(sudat[,8], sudat[,10] ), ]
View(sudat)
names(sudat)
sudat <- dplyr::arrange(PARENT_KEY, pair_id)
sudat <- sudat |> dplyr::arrange(PARENT_KEY, pair_id)
sudat$pair_id_rv <- as.integer(rep(1:13434, each=2))
## Estimate gender effect in the full sample
set.seed(1239)
out_model <-
factorEx::model_pAMCE(formula = outcome_binary_resp ~ gender + party + promises + education + profession,
data = sudat,
reg =  TRUE,
pair_id = sudat$pair_id_rv,
cluster_id = sudat$PARENT_KEY,
target_dist  = gender_dist_marginals,
target_type = "marginal",
boot = 2000)
gender_dist_marginals <- list(gender=c("Male"=0.76, "Female"=0.24),party=c("Independent"=0.56, "Minor"= 0.26, "Major"=0.26), promises= c("Boreholes"=0.33, "Education"=0.33, "Roads"= 0.33), education=c("Secondary"=0.55, "University"=0.45), profession=c("Teacher"=0.42, "Maize farmer"=0.05, "Major business owner"=0.53))
## Estimate gender effect in the full sample
set.seed(1239)
out_model <-
factorEx::model_pAMCE(formula = outcome_binary_resp ~ gender + party + promises + education + profession,
data = sudat,
reg =  TRUE,
pair_id = sudat$pair_id_rv,
cluster_id = sudat$PARENT_KEY,
target_dist  = gender_dist_marginals,
target_type = "marginal",
boot = 2000)
out_model_control <-
factorEx::model_pAMCE(formula = outcome_binary_resp ~ gender + party + promises + education + profession,
data = subdat_control,
reg =  TRUE,
pair_id = subdat_control$pair_id_rv,
cluster_id = subdat_control$PARENT_KEY,
target_dist  = gender_dist_marginals,
target_type = "marginal",
boot = 2000)
subdat_control <- sudat[sudat$tr_video_lv=="Control",]
subdat_progress <- sudat[sudat$tr_video_lv=="High viability",]
subdat_discr <- sudat[sudat$tr_video_lv=="Low viability",]
out_model_control <-
factorEx::model_pAMCE(formula = outcome_binary_resp ~ gender + party + promises + education + profession,
data = subdat_control,
reg =  TRUE,
pair_id = subdat_control$pair_id_rv,
cluster_id = subdat_control$PARENT_KEY,
target_dist  = gender_dist_marginals,
target_type = "marginal",
boot = 2000)
names(sudat)
gender_dist_marginals
out_model <-
factorEx::model_pAMCE(formula = outcome_binary_resp ~ gender + party + promises + education + profession,
data = sudat,
reg =  TRUE,
pair_id = sudat$pair_id_rv,
cluster_id = sudat$PARENT_KEY,
target_dist  = gender_dist_marginals,
target_type = "marginal",
boot = 2000)
glimpse(sudat)
out_model <-
factorEx::model_pAMCE(formula = outcome_binary_resp ~ gender + party + promises + education + profession,
data = sudat,
reg =  TRUE,
pair_id = sudat$pair_id_rv,
cluster_id = sudat$PARENT_KEY,
target_dist  = gender_dist_marginals,
target_type = "marginal",
boot = 2000)
gender_dist_marginals
table(sudat$gender)
table(sudat$education)
table(sudat$profession)
table(sudat$party)
table(sudat$promises)
gender_dist_marginals <- list(gender=c("Male"=0.76, "Female"=0.24),party=c("Independent"=0.56,  "Major"=0.26, "Minor"= 0.26), promises= c("Boreholes"=0.33, "Education"=0.33, "Roads"= 0.33), education=c("Secondary"=0.55, "University"=0.45), profession=c("Maize farmer"=0.05,"Major business owner"=0.53, "Teacher"=0.42 ))
out_model <-
factorEx::model_pAMCE(formula = outcome_binary_resp ~ gender + party + promises + education + profession,
data = sudat,
reg =  TRUE,
pair_id = sudat$pair_id_rv,
cluster_id = sudat$PARENT_KEY,
target_dist  = gender_dist_marginals,
target_type = "marginal",
boot = 2000)
out_model <-
factorEx::model_pAMCE(formula = outcome_binary_resp ~ gender + party + promises + education + profession,
factor_name ="gender",
data = sudat,
reg =  TRUE,
pair_id = sudat$pair_id_rv,
cluster_id = sudat$PARENT_KEY,
target_dist  = gender_dist_marginals,
target_type = "marginal",
boot = 2000)
out_model <-
factorEx::model_pAMCE(formula = outcome_binary_resp ~ gender + party + promises + education + profession,        data = sudat,
reg =  TRUE,
pair_id = sudat$pair_id_rv,
cluster_id = sudat$PARENT_KEY,
target_dist  = gender_dist_marginals,
target_type = "marginal",
boot = 2000)
master_ps <- read_csv("~/Dropbox/women_compaign_project/Paper1/jop_submission/revised_paper/replication_files/master_ps.csv")
names(master_ps)
gender_dist_marginals <- list(gender=c("Male"=0.76, "Female"=0.24),party=c("Independent"=0.56,  "Major"=0.26, "Minor"= 0.26), promises= c("Boreholes"=0.33, "Education"=0.33, "Roads"= 0.33), education=c("Secondary"=0.55, "University"=0.45), profession=c("Maize farmer"=0.05,"Major business owner"=0.53, "Teacher"=0.42 ))
sudat <- master_ps[,c("outcome_binary_resp","gender","party","promises","education","profession","KEY","PARENT_KEY","tr_video_lv")]
sudat <-na.omit(sudat)
sudat$gender <- factor(sudat$gender,levels = c("Male", "Female"))
sudat$party <- as.factor(sudat$party)
sudat$promises <- as.factor(sudat$promises)
sudat$education <- as.factor(sudat$education)
sudat$profession <- as.factor(sudat$profession)
sudat$pair_id <- as.integer(substr(sudat$KEY, nchar(sudat$KEY) - 1, nchar(sudat$KEY) - 1))
names(sudat)
# create id for all pairs in data
sudat <- sudat |> dplyr::arrange(PARENT_KEY, pair_id)
sudat$pair_id_rv <- as.integer(rep(1:13434, each=2))
## Estimate gender effect in the full sample
set.seed(1239)
out_model <-
factorEx::model_pAMCE(formula = outcome_binary_resp ~ gender + party + promises + education + profession,        data = sudat,
reg =  TRUE,
pair_id = sudat$pair_id_rv,
cluster_id = sudat$PARENT_KEY,
target_dist  = gender_dist_marginals,
target_type = "marginal",
boot = 2000)
out_model <-
factorEx::model_pAMCE(formula = outcome_binary_resp ~ gender + party + promises + education + profession,        data = sudat,
reg =  TRUE,
pair_id = sudat$pair_id_rv,
cluster_id = sudat$PARENT_KEY,
target_dist  = gender_dist_marginals,
target_type = "marginal",
boot = 2000)
